package vns.server;

import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;

import vns.client.service.RuleParamService;
import vns.server.domain.RuleParam;
import vns.shared.RuleActionDTO;
import vns.shared.RuleParamDTO;
import vns.shared.ObjectPropertyDTO;
import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class RuleParamServiceImpl extends RemoteServiceServlet implements
		RuleParamService {

	@Override
	public RuleParamDTO Create(RuleParamDTO paramDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		RuleParam actionparam = null;
		try {
			actionparam = new RuleParam(paramDTO);
			pm.makePersistent(actionparam);
		} finally {
			pm.close();
		}
		return actionparam.toDTO();
	}

	@Override
	public RuleParamDTO Load(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		RuleParamDTO actionparamDTO = null;
		try {
			RuleParam actionparam = pm.getObjectById(
					RuleParam.class, id);
			actionparamDTO = actionparam.toDTO();
		} finally {
			pm.close();
		}
		return actionparamDTO;
	}

	@Override
	public RuleParamDTO Update(RuleParamDTO actionparamDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		RuleParam actionparam = null;
		try {
			actionparam = pm.getObjectById(RuleParam.class,
					actionparamDTO.getId());
			actionparam.updatefromDTO(actionparamDTO);
		} finally {
			pm.close();
		}

		return actionparam.toDTO();
	}

	@Override
	public boolean Remove(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			RuleParam action = pm
					.getObjectById(RuleParam.class, id);
			if (action != null) {
				pm.deletePersistent(pm.getObjectById(RuleParam.class, id));
			}
		} finally {
			pm.close();
		}
		return true;
	}

	@Override
	public ArrayList<RuleParamDTO> Loadall(RuleActionDTO ruleActionDTO) {
		ArrayList<RuleParamDTO> listactionparamDTO = new ArrayList<RuleParamDTO>();
		// PersistenceManager pm=PMF.get().getPersistenceManager();
		// try{
		// RuleAction
		// ruleActions=pm.getObjectById(RuleAction.class,ruleActionDTO.getId());
		// }finally{
		// pm.close();
		// }
		return listactionparamDTO;
	}

	@Override
	public ArrayList<RuleParamDTO> Loadall(ObjectPropertyDTO opropertyDTO) {
		ArrayList<RuleParamDTO> listactionparamDTO = new ArrayList<RuleParamDTO>();
		// PersistenceManager pm=PMF.get().getPersistenceManager();
		// try{
		// ObjectProperty
		// actions=pm.getObjectById(ObjectProperty.class,opropertyDTO.getId());
		// Set<Key> actionparams=actions.getActionParam();
		// for(Key actionparam:actionparams ){
		// ActionParam term=pm.getObjectById(ActionParam.class,actionparam);;
		// listactionparamDTO.add(term.toDTO());
		// }
		// }finally{
		// pm.close();
		// }
		return listactionparamDTO;
	}

	@Override
	public void Updateall(List<RuleParamDTO> listNew, RuleActionDTO Action) {
		List<RuleParamDTO> listOld = this.Loadall(Action);
		for (RuleParamDTO element : listNew) {
			if (!containInList(listOld, element)) {
				element = this.Create(element);
			} else {
				this.Update(element);
			}
		}
		for (RuleParamDTO element : listOld) {
			if (!containInList(listNew, element)) {
				this.Remove(element.getId());
			}
		}

	}

	@Override
	public void Updateall(List<RuleParamDTO> listNew,
			ObjectPropertyDTO opropertyDTO) {
		List<RuleParamDTO> listOld = this.Loadall(opropertyDTO);
		for (RuleParamDTO element : listNew) {
			if (!containInList(listOld, element)) {
				element = this.Create(element);
			} else {
				this.Update(element);
			}
		}
		for (RuleParamDTO element : listOld) {
			if (!containInList(listNew, element)) {
				this.Remove(element.getId());
			}
		}

	}

	private boolean containInList(List<RuleParamDTO> list,
			RuleParamDTO element) {
		if (element.getId() == null)
			return false;
		for (RuleParamDTO optDTO : list) {
			if (optDTO.getId().equals(element.getId()))
				return true;
		}
		return false;
	}

}
